﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC4_Test.Models;

namespace MVC4_Test.Controllers
{
    public class QuestionTagController : Controller
    {
        private StackOverflowContext db = new StackOverflowContext();

        // ropet een question tag opt
        // GET: /QuestionTag/

        public ActionResult Index()
        {
            var questiontags = db.QuestionTags.Include(q => q.Question).Include(q => q.Tag);
            return View(questiontags.ToList());
        }

        //
        // GET: /QuestionTag/Details/5

        public ActionResult Details(int id = 0)
        {
            QuestionTag questiontag = db.QuestionTags.Find(id);
            if (questiontag == null)
            {
                return HttpNotFound();
            }
            return View(questiontag);
        }

        //
        // GET: /QuestionTag/Create

        public ActionResult Create()
        {
            ViewBag.QuestionId = new SelectList(db.Questions, "QuestionId", "Text");
            ViewBag.TagId = new SelectList(db.Tags, "TagId", "Text");
            return View();
        }

        //
        // POST: /QuestionTag/Create

        [HttpPost]
        public ActionResult Create(QuestionTag questiontag)
        {
            if (ModelState.IsValid)
            {
                db.QuestionTags.Add(questiontag);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.QuestionId = new SelectList(db.Questions, "QuestionId", "Text", questiontag.QuestionId);
            ViewBag.TagId = new SelectList(db.Tags, "TagId", "Text", questiontag.TagId);
            return View(questiontag);
        }

        //
        // GET: /QuestionTag/Edit/5

        public ActionResult Edit(int id = 0)
        {
            QuestionTag questiontag = db.QuestionTags.Find(id);
            if (questiontag == null)
            {
                return HttpNotFound();
            }
            ViewBag.QuestionId = new SelectList(db.Questions, "QuestionId", "Text", questiontag.QuestionId);
            ViewBag.TagId = new SelectList(db.Tags, "TagId", "Text", questiontag.TagId);
            return View(questiontag);
        }

        //
        // POST: /QuestionTag/Edit/5

        [HttpPost]
        public ActionResult Edit(QuestionTag questiontag)
        {
            if (ModelState.IsValid)
            {
                db.Entry(questiontag).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.QuestionId = new SelectList(db.Questions, "QuestionId", "Text", questiontag.QuestionId);
            ViewBag.TagId = new SelectList(db.Tags, "TagId", "Text", questiontag.TagId);
            return View(questiontag);
        }

        //
        // GET: /QuestionTag/Delete/5

        public ActionResult Delete(int id = 0)
        {
            QuestionTag questiontag = db.QuestionTags.Find(id);
            if (questiontag == null)
            {
                return HttpNotFound();
            }
            return View(questiontag);
        }

        //
        // POST: /QuestionTag/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            QuestionTag questiontag = db.QuestionTags.Find(id);
            db.QuestionTags.Remove(questiontag);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        // tag query systeem
        // Get /tags/1
        public ActionResult tags(int id = 0, string tab = "popular")
        {
            var result = db.Questions.Include("QuestionTag").ToList();


            switch (tab)
            {
                case "name":
                    result = db.Questions.Include("QuestionTag").Include("Answer").Include("Answer").ToList();
                    break;
                case "new":
                    result = db.Questions.Include("QuestionTag").OrderByDescending(p => p.Date).ToList();
                    break;
               default:
                    result = db.Questions.Include("QuestionTag").ToList();
                    break;

            }

            return View(result);
        }

        ////
        //// GET: /QuestionTag/Filter/id
        //public ActionResult Filter(string q)
        //{

        //    ViewBag.Query = q;

        //    var questiontags = db.QuestionTags.Select(p => p.Tag).Where(p => p.TagId.Contains(q) || q.Text.Contains(q));

           
        //    return View();
        //}
    }
}